/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.net.ConnectException;
import java.nio.channels.SocketChannel;
import java.util.Map.Entry;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import javax.net.SocketFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.ipc.VersionedProtocol;
import org.apache.hadoop.syscall.LinuxSystemCall;
import org.apache.hadoop.util.ReflectionUtils;

public class NetUtils {
  private static final Log LOG = LogFactory.getLog(NetUtils.class);
  // Insure that Network Control bits in sock will never be used,
  // allow to set them may cause issue on the network, killing network
  // control traffic.
  // NC bits are: 
  // in TOS Dec: 192 and up.
  public static final int IP_TOS_MAX_VALUE = 191;
  public static final int NOT_SET_IP_TOS = -1;
  public static final String DFS_CLIENT_TOS_CONF = "dfs.client.tos.value";

  private static Map<String, String> hostToResolved = 
      new HashMap<String, String>();

  /**
   * Check the tos value is valid, that is, in the -1~191 range
   * @param tosValue
   * @return
   */
  public static boolean isValidTOSValue(int tosValue) {
    return tosValue >= NOT_SET_IP_TOS && tosValue <= IP_TOS_MAX_VALUE;
  }
  
  /**
   * Get the socket factory for the given class according to its
   * configuration parameter
   * <tt>hadoop.rpc.socket.factory.class.&lt;ClassName&gt;</tt>. When no
   * such parameter exists then fall back on the default socket factory as
   * configured by <tt>hadoop.rpc.socket.factory.class.default</tt>. If
   * this default socket factory is not configured, then fall back on the JVM
   * default socket factory.
   * 
   * @param conf the configuration
   * @param clazz the class (usually a {@link VersionedProtocol})
   * @return a socket factory
   */
  public static SocketFactory getSocketFactory(Configuration conf,
      Class<?> clazz) {

    SocketFactory factory = null;

    String propValue =
        conf.get("hadoop.rpc.socket.factory.class." + clazz.getSimpleName());
    if ((propValue != null) && (propValue.length() > 0))
      factory = getSocketFactoryFromProperty(conf, propValue);

    if (factory == null)
      factory = getDefaultSocketFactory(conf);

    return factory;
  }

  /**
   * Get the default socket factory as specified by the configuration
   * parameter <tt>hadoop.rpc.socket.factory.default</tt>
   * 
   * @param conf the configuration
   * @return the default socket factory as specified in the configuration or
   *         the JVM default socket factory if the configuration does not
   *         contain a default socket factory property.
   */
  public static SocketFactory getDefaultSocketFactory(Configuration conf) {

    String propValue = conf.get("hadoop.rpc.socket.factory.class.default");
    if ((propValue == null) || (propValue.length() == 0))
      return SocketFactory.getDefault();

    return getSocketFactoryFromProperty(conf, propValue);
  }

  /**
   * Get the socket factory corresponding to the given proxy URI. If the
   * given proxy URI corresponds to an absence of configuration parameter,
   * returns null. If the URI is malformed raises an exception.
   * 
   * @param propValue the property which is the class name of the
   *        SocketFactory to instantiate; assumed non null and non empty.
   * @return a socket factory as defined in the property value.
   */
  public static SocketFactory getSocketFactoryFromProperty(
      Configuration conf, String propValue) {

    try {
      Class<?> theClass = conf.getClassByName(propValue);
      return (SocketFactory) ReflectionUtils.newInstance(theClass, conf);

    } catch (ClassNotFoundException cnfe) {
      throw new RuntimeException("Socket Factory class not found: " + cnfe);
    }
  }

  /**
   * Util method to build socket addr from either:
   *   <host>:<post>
   *   <fs>://<host>:<port>/<path>
   */
  public static InetSocketAddress createSocketAddr(String target) {
    return createSocketAddr(target, -1);
  }

  /**
   * Util method to build socket addr from either:
   *   <host>
   *   <host>:<post>
   *   <fs>://<host>:<port>/<path>
   */
  public static InetSocketAddress createSocketAddr(String target,
      int defaultPort) {
    // Format [IPv6 Address]:Port
    int colonIndex = target.indexOf(':');
    if (colonIndex < 0 && defaultPort == -1) {
      throw new RuntimeException("Not a host:port pair: " + target);
    }
    String hostname;
    int port = -1;
    if (!target.contains("/")) {
      target = "hdfs://" + target;
    }
    // a new uri
    URI addr = new Path(target).toUri();
    hostname = addr.getHost();
    port = addr.getPort();
    

    if (port == -1) {
      port = defaultPort;
    }

    if (getStaticResolution(hostname) != null) {
      hostname = getStaticResolution(hostname);
    }

    return InetSocketAddressFactory.createWithResolveRetry(hostname, port);
  }

  /**
   * Handle the transition from pairs of attributes specifying a host and port
   * to a single colon separated one.
   * @param conf the configuration to check
   * @param oldBindAddressName the old address attribute name
   * @param oldPortName the old port attribute name
   * @param newBindAddressName the new combined name
   * @return the complete address from the configuration
   */
  @Deprecated
  public static String getServerAddress(Configuration conf,
      String oldBindAddressName,
      String oldPortName,
      String newBindAddressName) {
    String oldAddr = conf.get(oldBindAddressName);
    int oldPort = conf.getInt(oldPortName, 0);
    String newAddrPort = conf.get(newBindAddressName);
    if (oldAddr == null && oldPort == 0) {
      return toIpPort(createSocketAddr(newAddrPort));
    }
    InetSocketAddress newAddr = NetUtils.createSocketAddr(newAddrPort);
    if (oldAddr == null) {
      oldAddr = newAddr.getAddress().getHostAddress();
    } else {
      LOG.warn("Configuration parameter " + oldBindAddressName +
          " is deprecated. Use " + newBindAddressName + " instead.");
    }
    if (oldPort == 0) {
      oldPort = newAddr.getPort();
    } else {
      LOG.warn("Configuration parameter " + oldPortName +
          " is deprecated. Use " + newBindAddressName + " instead.");      
    }
    try {
      return toIpPort(oldAddr, oldPort);
    } catch (UnknownHostException e) {
      LOG.error("DNS not supported.");
      LOG.fatal(e);
    }
    return oldAddr + ":" + oldPort;
  }

  /**
   * Adds a static resolution for host. This can be used for setting up
   * hostnames with names that are fake to point to a well known host. For e.g.
   * in some testcases we require to have daemons with different hostnames
   * running on the same machine. In order to create connections to these
   * daemons, one can set up mappings from those hostnames to "localhost".
   * {@link NetUtils#getStaticResolution(String)} can be used to query for
   * the actual hostname. 
   * @param host
   * @param resolvedName
   */
  public static void addStaticResolution(String host, String resolvedName) {
    synchronized (hostToResolved) {
      hostToResolved.put(host, resolvedName);
    }
  }

  /**
   * Retrieves the resolved name for the passed host. The resolved name must
   * have been set earlier using 
   * {@link NetUtils#addStaticResolution(String, String)}
   * @param host
   * @return the resolution
   */
  public static String getStaticResolution(String host) {
    synchronized (hostToResolved) {
      return hostToResolved.get(host);
    }
  }

  /**
   * This is used to get all the resolutions that were added using
   * {@link NetUtils#addStaticResolution(String, String)}. The return
   * value is a List each element of which contains an array of String 
   * of the form String[0]=hostname, String[1]=resolved-hostname
   * @return the list of resolutions
   */
  public static List <String[]> getAllStaticResolutions() {
    synchronized (hostToResolved) {
      Set <Entry <String, String>>entries = hostToResolved.entrySet();
      if (entries.size() == 0) {
        return null;
      }
      List <String[]> l = new ArrayList<String[]>(entries.size());
      for (Entry<String, String> e : entries) {
        l.add(new String[] {e.getKey(), e.getValue()});
      }
      return l;
    }
  }

  /**
   * Returns InetSocketAddress that a client can use to 
   * connect to the server. Server.getListenerAddress() is not correct when
   * the server binds to "0.0.0.0". This returns "127.0.0.1:port" when
   * the getListenerAddress() returns "0.0.0.0:port".
   * 
   * @param server
   * @return socket address that a client can use to connect to the server.
   */
  public static InetSocketAddress getConnectAddress(Server server) {
    InetSocketAddress addr = server.getListenerAddress();
    if (addr.getAddress().getHostAddress().equals("0.0.0.0")) {
      addr = new InetSocketAddress("127.0.0.1", addr.getPort());
    }
    return addr;
  }

  /**
   * Same as getInputStream(socket, socket.getSoTimeout()).<br><br>
   * 
   * From documentation for {@link #getInputStream(Socket, long)}:<br>
   * Returns InputStream for the socket. If the socket has an associated
   * SocketChannel then it returns a 
   * {@link SocketInputStream} with the given timeout. If the socket does not
   * have a channel, {@link Socket#getInputStream()} is returned. In the later
   * case, the timeout argument is ignored and the timeout set with 
   * {@link Socket#setSoTimeout(int)} applies for reads.<br><br>
   *
   * Any socket created using socket factories returned by {@link #NetUtils},
   * must use this interface instead of {@link Socket#getInputStream()}.
   *     
   * @see #getInputStream(Socket, long)
   * 
   * @param socket
   * @return InputStream for reading from the socket.
   * @throws IOException
   */
  public static InputStream getInputStream(Socket socket) 
      throws IOException {
    return getInputStream(socket, socket.getSoTimeout());
  }

  /**
   * Returns InputStream for the socket. If the socket has an associated
   * SocketChannel then it returns a 
   * {@link SocketInputStream} with the given timeout. If the socket does not
   * have a channel, {@link Socket#getInputStream()} is returned. In the later
   * case, the timeout argument is ignored and the timeout set with 
   * {@link Socket#setSoTimeout(int)} applies for reads.<br><br>
   * 
   * Any socket created using socket factories returned by {@link #NetUtils},
   * must use this interface instead of {@link Socket#getInputStream()}.
   *     
   * @see Socket#getChannel()
   * 
   * @param socket
   * @param timeout timeout in milliseconds. This may not always apply. zero
   *        for waiting as long as necessary.
   * @return InputStream for reading from the socket.
   * @throws IOException
   */
  public static InputStream getInputStream(Socket socket, long timeout) 
      throws IOException {
    return (socket.getChannel() == null) ? 
        socket.getInputStream() : new SocketInputStream(socket, timeout);
  }

  /**
   * Same as getOutputStream(socket, 0). Timeout of zero implies write will
   * wait until data is available.<br><br>
   * 
   * From documentation for {@link #getOutputStream(Socket, long)} : <br>
   * Returns OutputStream for the socket. If the socket has an associated
   * SocketChannel then it returns a 
   * {@link SocketOutputStream} with the given timeout. If the socket does not
   * have a channel, {@link Socket#getOutputStream()} is returned. In the later
   * case, the timeout argument is ignored and the write will wait until 
   * data is available.<br><br>
   * 
   * Any socket created using socket factories returned by {@link #NetUtils},
   * must use this interface instead of {@link Socket#getOutputStream()}.
   * 
   * @see #getOutputStream(Socket, long)
   * 
   * @param socket
   * @return OutputStream for writing to the socket.
   * @throws IOException
   */  
  public static OutputStream getOutputStream(Socket socket) 
      throws IOException {
    return getOutputStream(socket, 0);
  }

  /**
   * Returns OutputStream for the socket. If the socket has an associated
   * SocketChannel then it returns a 
   * {@link SocketOutputStream} with the given timeout. If the socket does not
   * have a channel, {@link Socket#getOutputStream()} is returned. In the later
   * case, the timeout argument is ignored and the write will wait until 
   * data is available.<br><br>
   * 
   * Any socket created using socket factories returned by {@link #NetUtils},
   * must use this interface instead of {@link Socket#getOutputStream()}.
   * 
   * @see Socket#getChannel()
   * 
   * @param socket
   * @param timeout timeout in milliseconds. This may not always apply. zero
   *        for waiting as long as necessary.
   * @return OutputStream for writing to the socket.
   * @throws IOException   
   */
  public static OutputStream getOutputStream(Socket socket, long timeout) 
      throws IOException {
    return (socket.getChannel() == null) ? 
        socket.getOutputStream() : new SocketOutputStream(socket, timeout);            
  }

  /**
   * This is a drop-in replacement for 
   * {@link Socket#connect(SocketAddress, int)}.
   * In the case of normal sockets that don't have associated channels, this 
   * just invokes <code>socket.connect(endpoint, timeout)</code>. If 
   * <code>socket.getChannel()</code> returns a non-null channel,
   * connect is implemented using Hadoop's selectors. This is done mainly
   * to avoid Sun's connect implementation from creating thread-local 
   * selectors, since Hadoop does not have control on when these are closed
   * and could end up taking all the available file descriptors.
   * 
   * @see java.net.Socket#connect(java.net.SocketAddress, int)
   * 
   * @param socket
   * @param endpoint 
   * @param timeout - timeout in milliseconds
   */
  public static void connect(Socket socket, 
      SocketAddress endpoint, 
      int timeout) throws IOException {
    connect(socket, endpoint, timeout, NOT_SET_IP_TOS);
  }

  public static void connect(Socket socket, 
      SocketAddress endpoint, 
      int timeout, 
      int ipTosValue) throws IOException {
    if (socket == null || endpoint == null || timeout < 0 
        || ipTosValue < NOT_SET_IP_TOS 
        || ipTosValue > IP_TOS_MAX_VALUE) {
      throw new IllegalArgumentException("Illegal argument for connect()");
    }

    SocketChannel ch = socket.getChannel();

    if (ch == null) {
      // let the default implementation handle it.
      socket.connect(endpoint, timeout);
    } else {
      // set the socket IP_TOS value
      if (ipTosValue != NOT_SET_IP_TOS) {
        LinuxSystemCall.setIPTOSVal(ch, ipTosValue);
      }
      SocketIOWithTimeout.connect(ch, endpoint, timeout);
    }

    // There is a very rare case allowed by the TCP specification, such that
    // if we are trying to connect to an endpoint on the local machine,
    // and we end up choosing an ephemeral port equal to the destination port,
    // we will actually end up getting connected to ourself (ie any data we
    // send just comes right back). This is only possible if the target
    // daemon is down, so we'll treat it like connection refused.
    if (socket.getLocalPort() == socket.getPort() &&
        socket.getLocalAddress().equals(socket.getInetAddress())) {
      LOG.info("Detected a loopback TCP socket, disconnecting it");
      socket.close();
      throw new ConnectException(
          "Localhost targeted connection resulted in a loopback. " +
          "No daemon is listening on the target port.");
    }
  }

  /** 
   * Given a string representation of a host, return its ip address
   * in textual presentation.
   * 
   * @param name a string representation of a host:
   *             either a textual representation its IP address or its host name
   * @return its IP address in the string format
   */
  public static String normalizeHostName(String name) {
    try {
      return InetAddress.getByName(name).getHostAddress();
    } catch (UnknownHostException e) {
      return name;
    }
  }

  /** 
   * Given a collection of string representation of hosts, return a list of
   * corresponding IP addresses in the textual representation.
   * 
   * @param names a collection of string representations of hosts
   * @return a list of corresponding IP addresses in the string format
   * @see #normalizeHostName(String)
   */
  public static List<String> normalizeHostNames(Collection<String> names) {
    List<String> resolvedIpAddresses = new ArrayList<String>(names.size());
    for (String name : names) {
      resolvedIpAddresses.add(normalizeHostName(name));
    }
    return resolvedIpAddresses;
  }

  final static Map<InetAddress, Boolean> knownLocalAddrs = new ConcurrentHashMap<InetAddress, Boolean>();

  public static boolean isLocalAddressWithCaching(InetAddress addr) {
    if (knownLocalAddrs.containsKey(addr)) {
      return true;
    }
    if (isLocalAddress(addr)) {
      // add the address to known local address list
      knownLocalAddrs.put(addr, Boolean.TRUE);
      return true;
    } else {
      return false;
    }
  }

  public static boolean isLocalAddress(InetAddress addr) {
    // Check if the address is any local or loop back
    boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
    // Check if the address is defined on any interface
    if (!local) {
      try {
        local = NetworkInterface.getByInetAddress(addr) != null;
      } catch (SocketException e) {
        local = false;
      }
    }   
    return local;
  }

  public static int getIPTOS(Socket socket) throws IOException {
    return LinuxSystemCall.getIPTOSVal(socket);
  }

  public static int getIPTOS(SocketChannel socketChannel) throws IOException {
    return LinuxSystemCall.getIPTOSVal(socketChannel);
  }

  public static InetSocketAddress resolveAddress(InetSocketAddress address) {
    if(wasInitializedWithIp(address.getAddress())) {
      // No need to resolve it when initialized with an IP address.
      return address;
    }
    String hostName = address.getHostName() + ":" + address.getPort();
    InetSocketAddress newAddr = createSocketAddr(hostName);
    if (newAddr.isUnresolved()) {
      LOG.warn("Address unresolvable: " + newAddr);
    } else if (!newAddr.equals(address)) {
      LOG.info("DNS changed from " + address + " to " + newAddr);
      return newAddr;
    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug("DNS Address unchanged: " + address);
      }
    }
    return null;
  }

  public static String getSrcNameFromSocketChannel(SocketChannel channel) {
    String src = "UNKNOWN";
    Socket socket = null;
    if (channel != null) {
      socket = channel.socket();
      if (socket != null) {
        src = socket.toString();
      }
    }
    return src;
  }
  
  public static boolean wasInitializedWithIp(InetAddress address) {
    return address.toString().startsWith("/");
  }
  
  public static boolean wasInitializedWithHostname(InetAddress address) {
    return !wasInitializedWithIp(address);
  }

  
  public static String toIpPort(InetSocketAddress addr) {
    if (addr == null) {
      return null;
    }
    return getHostAddress(addr.getAddress()) + ":" + addr.getPort();
  }
  
  public static String toIpPort(String ipAddress, int port) throws UnknownHostException {
    if (ipAddress == null) {
      return null;
    }
    InetAddress addr = InetAddress.getByName(ipAddress.trim());
    return getHostAddress(addr) + ":" + port;
  }
  
  public static String getHostAddress(InetAddress addr) {
    if(addr == null) {
      return null;
    }
    String ipAddress = addr.getHostAddress();
    if (addr instanceof Inet6Address) {
      ipAddress = "[" + ipAddress + "]";
    }
    return ipAddress;
  }

  /**
   * Tries to bind to the given address. Throws an exception on failure.
   * Used to fail earlier and verify configuration values.
   */
  public static void isSocketBindable(InetSocketAddress addr)
      throws IOException {
    if (addr == null) {
      return;
    }
    ServerSocket socket = new ServerSocket();
    try {
      socket.bind(addr);
    } finally {
      socket.close();
    }
  }
}
